package com.rom.linkedlist;

/**
@author :YLNOFI
@since :2023/11/3-22:17
@deprecated :环形单链表
 */
public class Josepfu {
    public static void main(String[] args) {
        //创建一个first节点、当前没有编号
        CircleSingleLinkedList linkedList = new CircleSingleLinkedList();
        System.out.println("添加-----");
        linkedList.addBoy(5);
        System.out.println("展示-----");
        linkedList.showBoy();
        System.out.println(linkedList.size());
        System.out.println("跳出");
        linkedList.countBoy(1,4, linkedList.size());
    }
}

//环形链表
class CircleSingleLinkedList{

    private Boy first = null;

    public void addBoy(int num){

        if(num < 1){
            System.out.println("num值不正确");
            return;
        }
        Boy curboy = null;
        for (int i = 1; i <= num; i++) {
            Boy boy = new Boy(i);
            if(i == 1){
                first = boy;
                first.setNext(first);//构成环
                curboy = first;
            }else{
                curboy.setNext(boy);
                boy.setNext(first);
                curboy = boy;
            }
        }

    }
    public int size(){
        Boy curboy = first;
        int size = 0;
        if(curboy==null){
            System.out.println("为空");
            return size;
        }
        while (true){
            curboy = curboy.getNext();
            size++;
            if(curboy == first){
                return size;
            }

        }
    }
    //遍历
    public void showBoy(){
        Boy curboy = first;
        if(curboy==null){
            System.out.println("为空");
            return;
        }
        while (true){
            System.out.println("No."+curboy.getNo());
            curboy = curboy.getNext();
            if(curboy == first){
                break;
            }
        }
    }

    /**
     * @param startNo 表示第几个开始数
     * @param countNum  表示数几下
     * @param nums  最初有多少个节点
     */
    public void countBoy(int startNo,int countNum,int nums){
        Boy curboy = first;
//        1.找到开始的节点
        for (int i = 1; i < startNo; i++) {
            curboy = curboy.getNext();
        }

        while (true){
            if(curboy.getNext() == curboy){
                System.out.println("No."+curboy.getNext().getNo());
                break; //出来的条件
            }
            for (int j = 1; j < countNum; j++) {
                if(j + 1 == countNum){  //寻找要删除的节点。
                    //跳过
                    System.out.println("No."+curboy.getNext().getNo());
                    curboy.setNext(curboy.getNext().getNext());
                }
                curboy = curboy.getNext();
            }
        }


    }
}

class Boy{

    private int no;
    private Boy next;

    public Boy(int no) {
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public Boy getNext() {
        return next;
    }

    public void setNext(Boy next) {
        this.next = next;
    }

    @Override
    public String toString() {
        return "Boy{" +
                "no=" + no +
                '}';
    }
}